﻿using System;
using System.Collections.Generic;
using System.IO;


namespace CCPSIP
{
    public class CloudDiskKit
    {
        private static string[] kUserRootDirs = { @"\\192.168.19.1\iSender_D" };
        private const string kWindowsUserId = "ccpsip_cloud";
        private static byte[] kWindowsUserKey = { 0x59, 0x4C, 0x43, 0x71, 0x6A,
                                              0x6D, 0x44, 0x6A, 0x32, 0x5A,
                                              0x72, 0x62, 0x72, 0x43, 0x39,
                                              0x68, 0x51, 0x6E, 0x63, 0x6B,
                                              0x57, 0x32, 0x64, 0x74, 0x62
                                            };
        private const string kDirNameDoc = "Documents";
        private const string kDirNameSticker = "Stickers";


        /// <summary>建立 Cloud Disk User 資料夾</summary>
        public static void InitUserFolder(string SIP_Num)
        {
            //【01】組合資料夾路徑
            //
            string UserRootDir = kUserRootDirs[0];

            string userFolder = SIP_Num;
            if (SIP_Num.Length < 7)
            {
                userFolder = SIP_Num.Substring(2, 3);
            }

            string userPath = string.Format(@"{0}\{1}", UserRootDir, userFolder);
            string dirPathDoc = string.Format(@"{0}\{1}", userPath, kDirNameDoc);
            string dirPathSticker = string.Format(@"{0}\{1}", userPath, kDirNameSticker);


            //【02】切換登入身分，建立資料夾
            //
            string tempCode = System.Text.Encoding.UTF8.GetString(kWindowsUserKey);
            using (new Tools.Impersonator(kWindowsUserId, "", tempCode))
            {
                if (!Directory.Exists(dirPathDoc))
                {
                    Directory.CreateDirectory(dirPathDoc);  //會建sip_num的目錄與Document目錄
                }

                if (!Directory.Exists(dirPathSticker))
                {
                    Directory.CreateDirectory(dirPathSticker);  //會建Sticker目錄
                }
            }
        }

        /// <summary>檢查Cloud Disk User資料夾是否存在</summary>
        public static bool HasUserFolder(string SIP_Num)
        {
            //【01】組合資料夾路徑
            string UserRootDir = kUserRootDirs[0];

            string userFolder = SIP_Num;
            if (SIP_Num.Length < 7)
            {
                userFolder = SIP_Num.Substring(2, 3);
            }

            string userPath = string.Format(@"{0}\{1}", UserRootDir, userFolder);

            //【02】切換登入身分，檢查資料夾是否存在
            string tempCode = System.Text.Encoding.UTF8.GetString(kWindowsUserKey);
            using (new Tools.Impersonator(kWindowsUserId, "", tempCode))
            {
                if (Directory.Exists(userPath))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>刪除 Cloud Disk User資料夾</summary>
        public static bool DeleteUserFolder(string SIP_Num)
        {
            //【01】組合資料夾路徑
            string UserRootDir = kUserRootDirs[0];

            string userFolder = SIP_Num;
            if (SIP_Num.Length < 7)
            {
                userFolder = SIP_Num.Substring(2, 3);
            }

            string userPath = string.Format(@"{0}\{1}", UserRootDir, userFolder);

            //【02】切換登入身分，刪除資料夾
            string tempCode = System.Text.Encoding.UTF8.GetString(kWindowsUserKey);
            using (new Tools.Impersonator(kWindowsUserId, "", tempCode))
            {
                if (Directory.Exists(userPath))
                {
                    Directory.Delete(userPath, true);
                }
            }

            return true;
        }
    }
}